package com.jcl.recruit.console.db.logic;

import com.jcl.core.constants.Constant;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.console.ConsoleDbCorpDao;
import com.jcl.recruit.console.ConsoleDbServerDao;
import com.jcl.recruit.console.mapper.ConsoleDbCorpMapper;
import com.jcl.recruit.console.model.ConsoleDbServer;
import com.jcl.recruit.console.vo.ConsoleDbServerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DbServerLogic
 * @Author yyliu
 * @Date 2018/12/21 15:12
 * @Version 1.0
 **/
@Component
public class DbServerLogic {

    @Autowired
    private ConsoleDbServerDao consoleDbServerDao;

    @Autowired
    private ConsoleDbCorpDao consoleDbCorpDao;


    public List<ConsoleDbServer> findList() {
        List<ConsoleDbServer> list = consoleDbServerDao.findList();
        return list;
    }

    public List<ConsoleDbServer> findListByPrefix(String prefix) {
        List<ConsoleDbServer> list = consoleDbServerDao.findListByPrefix(prefix);
        return list;
    }

    public List<String> getServerNameList() {
        List<String> serverNameList = new ArrayList<>();
        List<ConsoleDbServer> list = consoleDbServerDao.findList();
        if(CollectionUtils.isNotEmpty(list)) {
            for(ConsoleDbServer dbServer: list){
                serverNameList.add(dbServer.getServerName());
            }
        }
        return serverNameList;
    }

    public ConsoleDbServer findOne(Integer id) {
        return consoleDbServerDao.findOne(id);
    }

    public ConsoleDbServer findOneByServerName(String serverName) {
        return consoleDbServerDao.findOneByServerName(serverName);
    }


    public Map<String, Integer> prefixMap() {
        List<ConsoleDbServer> serverList = consoleDbServerDao.findList();
        return prefixMap(serverList);
    }

    public Map<String, Integer> prefixMap(List<ConsoleDbServer> serverList) {
        Map<String, Integer> prefixMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(serverList)) {
            for (ConsoleDbServer server : serverList) {
                Integer corpTotal = prefixMap.get(server.getServerName());
                if (corpTotal == null) {
                    corpTotal = 0;
                }
                if (server.getTotal() < Constant.CORP_TOTAL) {
                    corpTotal += Constant.CORP_TOTAL;
                } else {
                    corpTotal += server.getTotal();
                }
                prefixMap.put(server.getServerName(), corpTotal);
            }
        }

        return prefixMap;
    }


    public List<ConsoleDbServerVo> serverList(List<ConsoleDbServer> list) {
        List<ConsoleDbServerVo> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            List<Map<String, Object>> numberList = consoleDbCorpDao.findServerNumber();
            Map<String, Integer> serverNumber = new HashMap<>();
            if (CollectionUtils.isNotEmpty(list)) {
                for (Map<String, Object> map : numberList) {
                    serverNumber.put(map.get("serverName").toString(), Integer.valueOf(map.get("number").toString()));
                }
            }
            for (ConsoleDbServer server : list) {
                ConsoleDbServerVo vo = new ConsoleDbServerVo();
                BeanUtils.copyProperties(server, vo);
                vo.setDbPassword(null);
                vo.setCorpUsed(serverNumber.get(server.getServerName()));
                voList.add(vo);
            }
        }
        return voList;
    }


    public ConsoleDbServer add(ConsoleDbServerVo vo) {

        ConsoleDbServer server = new ConsoleDbServer();
        BeanUtils.copyProperties(vo, server);
        server.setTotal(Constant.CORP_TOTAL);
        if (vo.getCorpNumber() > Constant.CORP_TOTAL) {
            vo.setCorpNumber(Constant.CORP_TOTAL);
        }
        server.setStatus(0);
        server.setCorpEnd(vo.getCorpStart() + vo.getCorpNumber() - 1);
        consoleDbServerDao.add(server);


        return server;
    }


    public ConsoleDbServer update(ConsoleDbServer server) {

        consoleDbServerDao.update(server);
        return server;
    }


    public ConsoleDbServer save(ConsoleDbServer server) {

        consoleDbServerDao.add(server);
        return server;
    }

}
